﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSysCore;

namespace HSchSchedule.FrontPage
{
    public partial class CustomerManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            List<Customer> lst = DataBase.GetAllCustomer();
            gridViewCustomer.DataSource = lst;
            gridViewCustomer.DataBind();
        }

        protected void gridViewCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Customer enCus = (Customer)e.Row.DataItem;

                Label lblID = (Label)e.Row.FindControl("lblCustomerID");
                lblID.Text = enCus.CustomerId.ToString();

                Label lblCustomerName = (Label)e.Row.FindControl("lblCustomerName");
                lblCustomerName.Text = enCus.CustomerName;

                Label lblCustomerAddress = (Label)e.Row.FindControl("lblCustomerAddress");
                lblCustomerAddress.Text = enCus.CustomerAddress + enCus.CustomerDistrictOrCity + enCus.Province;

                Label lblPhone = (Label)e.Row.FindControl("lblPhone");
                lblPhone.Text = enCus.CustomerPhone;

                Label lblContactName = (Label)e.Row.FindControl("lblContactName");
                lblContactName.Text = enCus.ContactName;

                Label lblContactPhone = (Label)e.Row.FindControl("lblContactPhone");
                lblContactPhone.Text = enCus.ContactPhone;

                HyperLink hplUpdate = (HyperLink)e.Row.FindControl("hplUpdate");
                hplUpdate.NavigateUrl = "~/FrontPage/UpdateCustomer.aspx?CustomerID=" + enCus.CustomerId;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in gridViewCustomer.Rows)
            {
                CheckBox chkDelete = (CheckBox)r.FindControl("chkDelete");
                if (chkDelete.Checked)
                {
                    Label lblCustomerID = (Label)r.FindControl("lblCustomerID");
                    int cusID = int.Parse(lblCustomerID.Text);
                    try
                    {
                        DataBase.DeleteCustomer(cusID);
                    }
                    catch
                    {
                        lblError.Text = "Xóa không thành công!";
                    }
                }
            }

            Response.Redirect("~/FrontPage/CustomerManage.aspx");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/FrontPage/AddNewCustomer.aspx");
        }

    }
}
